package com.hydl.power.service.impl;

import com.hydl.power.mapper.NavbarMapper;
import com.hydl.power.mapper.OrderDeliveryMenuMapper;
import com.hydl.power.pojo.Navbar;
import com.hydl.power.pojo.NavbarVO;
import com.hydl.power.pojo.OrderDeliveryMenu;
import com.hydl.power.service.NavBarService;
import com.hydl.power.utils.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author wxb
 * @date 2020/3/21 16:37
 */
@Service
public class NavBarServiceImpl implements NavBarService {

    @Autowired
    private NavbarMapper navbarMapper;
    @Autowired
    private OrderDeliveryMenuMapper orderDeliveryMenuMapper;

    @Override
    public int deleteById(String navbarId) {
        int i = navbarMapper.deleteNavbarById(navbarId);
        if (i == 1){
            return orderDeliveryMenuMapper.deleteByPrimaryKey(navbarId);
        }else {
            return 0;
        }
        //return navbarMapper.deleteNavbarById(navbarId);
    }

    @Override
    public int updateById(Integer id,String navName) {
        return navbarMapper.updateById(id, navName);
    }

    @Override
    public int addNavbar(String navName,Integer id) {
        Navbar navbar = navbarMapper.selectNavbarById(id);
        if (navbar == null) {
            Navbar navbar1 = new Navbar();
            navbar1.setNavName(navName);
            navbar1.setType(1);
            navbar1.setpId(0);
            navbar1.setNavbarId(IdUtil.getRandomString(8));
            navbarMapper.insert(navbar1);
            //return navbarMapper.insert(navbar1);
            OrderDeliveryMenu orderDeliveryMenu = new OrderDeliveryMenu();
            orderDeliveryMenu.setName(navName);
            orderDeliveryMenu.setNavId(navbar1.getNavbarId());
            orderDeliveryMenu.setpId("0");
            return orderDeliveryMenuMapper.insert(orderDeliveryMenu);
        } else {
            Navbar navbar2 = new Navbar();
            navbar2.setNavName(navName);
            navbar2.setpId(navbar.getId());
            navbar2.setpName(navbar.getNavName());
            navbar2.setType(2);
            navbar2.setNavbarId(IdUtil.getRandomString(8));
             navbarMapper.insert(navbar2);

            OrderDeliveryMenu orderDeliveryMenu1 = orderDeliveryMenuMapper.getOrderDeliveryMenuByNavId(navbar.getNavbarId());
            OrderDeliveryMenu orderDeliveryMenu = new OrderDeliveryMenu();
            orderDeliveryMenu.setName(navName);
            orderDeliveryMenu.setNavId(navbar2.getNavbarId());
            //orderDeliveryMenu.setpId(orderDeliveryMenu1.getId());
            orderDeliveryMenu.setpId(orderDeliveryMenu1.getNavId());
            orderDeliveryMenu.setpName(navbar.getNavName());
            return orderDeliveryMenuMapper.insert(orderDeliveryMenu);
        }
    }
    @Override
    public Set<NavbarVO> getNavbar() {
        List<Navbar> list = navbarMapper.getAll();
        if (list == null || list.isEmpty()) {
            return null;
        }
        Set<NavbarVO> navbarVOList = new LinkedHashSet<>();
        Map<Integer, NavbarVO> map1 = new LinkedHashMap<>(32);
        Map<Integer, Navbar> map2 = new LinkedHashMap<>(32);
        for (Navbar navbar : list) {
            if (navbar.getpId().equals(0)) {
                map1.put(navbar.getId(), cast(navbar, navbar.getId()));
            } else {
                map2.put(navbar.getId(), navbar);
            }
        }
        if (!map2.isEmpty()) {
            for (Integer str : map2.keySet()) {
                Navbar navbar = map2.get(str);
                NavbarVO vo = map1.get(navbar.getpId());
                if (vo.getChilds() == null || vo.getChilds().isEmpty()) {
                    LinkedHashSet<NavbarVO> set = new LinkedHashSet<>();
                    set.add(cast(navbar, navbar.getId()));
                    vo.setChilds(set);
                } else {
                    vo.getChilds().add(cast(navbar, navbar.getId()));
                }
            }
        }
        for (Integer str : map1.keySet()) {
            navbarVOList.add(map1.get(str));
        }
        return navbarVOList;
    }

    private static NavbarVO cast(Navbar navbar, Integer id) {
        NavbarVO vo = new NavbarVO();
        vo.setId(navbar.getId());
        vo.setNavName(navbar.getNavName());
        vo.setType(navbar.getType());
        vo.setNavbarId(navbar.getNavbarId());
        vo.setOpen("");
        if (id.equals(navbar.getId())) {
            vo.setActive("active");
        } else {
            vo.setActive("");
        }
        return vo;
    }
}
